/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Drawing;
using Cafechess.Imaging;
namespace Cafechess.Chess.Controls
{
	/// <summary>
	/// The layer that is responsible for drawing the boarder of the
	/// chessboard, including the algebraic row/column designations.
	/// </summary>
  public class BoarderLayer: Layer
  {
    protected int coBoarderWidth;
    Color coBoarderColor;

    public Color BoarderColor
    {
      get{ return coBoarderColor; }
    }
    public int BoarderWidth
    {
      get{ return coBoarderWidth; }
      set{ coBoarderWidth = value;}
    }
    
    public override Rectangle Square
    {
      get
      {
        return base.Square;
      }
      set
      {
        base.Square = value;
        GenerateBoarders();
      }
    }
    /// <summary>
    /// Construct our surface object that will manage the physical drawing bitmap
    /// and the order in which our layers are applied to the image.
    /// </summary>
    /// <param name="rectangle"></param>
    public BoarderLayer(Rectangle rectangle, Color background) : base(rectangle,background)
    {
      coBoarderColor = Color.Beige;
      GenerateBoarders();
    }
    
    
    public Color Color
    {
      get{return coBoarderColor;}
      set
      {
        coBoarderColor = value;
        GenerateBoarders();
        Parent.IsRedraw = true;
        IsRedraw = true;
      }
    }

    static string [] coNumbers = { "8","7","6","5","4","3","2","1" };
    static string [] coLetters = { "a","b","c","d","e","f","g","h" };
    public void GenerateBoarders()
    {
      Clear();
      ChessSurface cs = (ChessSurface)Parent;
      if( coBoarderWidth > 0 )
      {
        Font font = new Font(FontFamily.GenericMonospace,10);
        RectangleF boardSquare = new RectangleF(0, Square.Y + coBoarderWidth,
          Square.Height - (coBoarderWidth * 2),
          Square.Width  - (coBoarderWidth * 2));       
        boardSquare.Height = boardSquare.Height / 8;
        boardSquare.Width = coBoarderWidth;        
        Brush aBrush = new SolidBrush(coBoarderColor);
        Bitmap bm = new Bitmap( Square.Width,Square.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        Graphics offScreenDC = Graphics.FromImage(bm);
        offScreenDC.FillRectangle(aBrush,0,0,Square.Width,Square.Height);
        Brush aFontBrush = new SolidBrush(Color.Black);
        // Set format of string.
        StringFormat drawFormat = new StringFormat();
        drawFormat.Alignment = StringAlignment.Center;
        drawFormat.LineAlignment = StringAlignment.Center;
        if( cs.IsFlipped )
        {
          for(int ndx=7; ndx >= 0; ndx--)
          {
            offScreenDC.DrawString(coNumbers[ndx],font,aFontBrush,boardSquare,drawFormat);
            boardSquare.Offset(0,boardSquare.Height);
          }
        }
        else
        {
          for(int ndx=0; ndx < 8; ndx++)
          {
            offScreenDC.DrawString(coNumbers[ndx],font,aFontBrush,boardSquare,drawFormat);
            boardSquare.Offset(0,boardSquare.Height);
          }
        }
        boardSquare.Width = boardSquare.Height;
        boardSquare.Height = coBoarderWidth;
        boardSquare.X = coBoarderWidth;
        boardSquare.Y = Square.Height - coBoarderWidth;
        if( cs.IsFlipped )
        {
          for(int ndx=7; ndx >= 0; ndx--)
          {
            offScreenDC.DrawString(coLetters[ndx],font,aFontBrush,boardSquare,drawFormat);
            boardSquare.Offset(boardSquare.Width,0);
          }
        }
        else
        {
          for(int ndx=0; ndx < 8; ndx++)
          {
            offScreenDC.DrawString(coLetters[ndx],font,aFontBrush,boardSquare,drawFormat);
            boardSquare.Offset(boardSquare.Width,0);
          }
        }
        DrawableBitmap db = new DrawableBitmap("left");
        db.Drawing = bm;
        db.Square = Square;
        // Make sure this image cleans itself up.
        db.IsResouceOwner = true;
        Add(db);
        aBrush.Dispose();
        aFontBrush.Dispose();
        font.Dispose();
        offScreenDC.Dispose();
      }
    }   
	}

}
